﻿using System;
using System.IO;
using System.Windows.Markup;
using System.Collections.Generic;
using Petra.Gis.Geometry.Support;
using Petra.Gis.RenderingServices.GDIPlus;
using Petra.Gis.RenderingServices.PlanarBase;
using Petra.Gis.ReferenceSystemServices;
using Petra.Gis.DataServices.SHP;
using Petra.Gis.DataServices.GDO.Sqlserver;

namespace testGridMap
{
  public static class testRaster
  {


    public static void testRasterTransformed()
    {

      BitmapMap map = new BitmapMap()
      {
        Width = 800,
        Height = 600,
        //ProjectedCoordinateSystem = (ProjectedCoordinateSystem)SearchCoordinateSystem.ByAuthorityAndCode("EPSG", "2065", "metre"),
        //ProjectionExtent = new BoundingBox(436500, 1139999, 434000, 1137999),
        ProjectedCoordinateSystem = (ProjectedCoordinateSystem)Petra.Gis.Environment.SearchSrsBy("INGR", "49", "metre"),
        ProjectionExtent = new BoundingBox(-436500, -1139999, -434000, -1137999),
        //ProjectedCoordinateSystem = (ProjectedCoordinateSystem)SearchCoordinateSystem.ByAuthorityAndCode("EPSG", "32662", "projdegree"),
        //ProjectionExtent = new BoundingBox(18.797204785644, 49.5163167473756, 18.8294604740699, 49.5360093650922),
        NominalScale = 5000,
        ApplyMapRangeAsSpatialFilter = true,
        Scale = 15000
      };

      // test xaml
      //string outStr = XamlWriter.Save(map);

      // prepare some data...
      // prepare and open connection to spatial database
      // mssql server
      GdoSqlConnection c = new GdoSqlConnection()
      {
        ConnectionString =
        @"Data Source=localhost; 
         Initial Catalog=TEST_RASTRY;
				 Integrated Security = True;
         MultipleActiveResultSets=True",
        Name = "raster maps"
      };

      c.Open();

      // prepare command
      GdoSqlCommand cmd = new GdoSqlCommand();
      cmd.Connection = c;
      cmd.CommandType = System.Data.CommandType.Text;
      cmd.CommandText = "select * from [TEST_RASTRY].[dbo].[GR_PORS]";

      // prepare renderer
      GridRenderer g = new GridRenderer()
      {
        SpatialFieldName = "Geometry", //optional, if user want to limit renderer to only one field
        Style = new Petra.Gis.RenderingServices.GDIPlus.Style()
        {
          Enabled = true,
          StyleScaleType = Petra.Gis.RenderingServices.PlanarBase.StyleScaleTypeEnum.ScaleIndependent,
          StyleComponents = new Petra.Gis.RenderingServices.PlanarBase.StyleComponents()
          {
            {"Brightness", 0},
            {"contrast", 0}
          }
        }
      };

      CommandLegendNode l = map.Legend.CreateLegendNode(cmd, g);

      map.Legend.ChildNodes.Add(l);

      System.Drawing.Bitmap b = map.Draw();

      b.Save("c:\\temp\\prvni.png", System.Drawing.Imaging.ImageFormat.Png);

      // test save xaml map definition
      //outStr = XamlWriter.Save(map);
    }

  }
}
